class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> vec(ratings.size(), 0);
        vec[0] = 1;
        for (int i = 1; i < ratings.size(); i++)
        {
            // for(auto n:vec)
            //     cout<<n<<endl;
            // cout<<i<<" "<<ratings.size()<<endl;
            if (ratings[i] > ratings[i - 1])
                vec[i] = vec[i - 1] + 1;
            else if (ratings[i] < ratings[i - 1])
            {
                int count = 0;
                while (i < ratings.size() && ratings[i] < ratings[i - 1])
                {
                    count++;
                    i++;
                }
                for (int j = 1; j <= count; j++)
                {
                    vec[i - j] = j;
                }
                if (vec[i - count - 1] <= count)
                    vec[i - count - 1]  = count + 1;
                i--; //关键！！与for循环括号里的i++抵消一下
            }
            else
                vec[i] = 1;
            // for(auto n:vec)
            //     cout<<n<<endl;
            // cout<<i<<" "<<ratings.size()<<endl;
        }
        return accumulate(vec.begin(), vec.end(), 0);
    }
};
